
//-----------------MATRICES---------------
float4x4 xWorld: World;				
float4x4 xView: View;
float4x4 xProjection: Projection;

//-----------------CONSTANTS----------------

float4 xLightPosition;

float3 ambientI;
float3 diffuseI;
float3 specularI;

float3 ambientK;
float3 diffuseK;
float3 specularK;

float alpha;

float4 eye;

//------------------- STRUCTS -------------------   


struct BufferToVertex
{
float3 Pos : POSITION;
float4 Normal : NORMAL;
float4 UV : TEXCOORD0;
};


struct VertexToPixel
{
float4 Pos:POSITION;
float4 UV:TEXCOORD0;
float4 Pos3D:TEXCOORD1;
float4 Normal:TEXCOORD2;
};

struct PixelToFrame
{
float4 Color:COLOR;
};

//---------------------- SHADERS ------------------------

VertexToPixel lightedVS(BufferToVertex IN)
{
  VertexToPixel OUT = (VertexToPixel)0;
  float4x4 WorldViewProj;
  
  WorldViewProj=mul(xWorld,xView);
  WorldViewProj=mul(WorldViewProj,xProjection);
  
  OUT.Pos=mul(float4(IN.Pos.xyz,1.0), WorldViewProj);

  OUT.Pos3D=mul(float4(IN.Pos.xyz,1.0),xWorld);  
  OUT.Normal=mul(float4(IN.Normal.xyz,1.0), xWorld);
  OUT.UV=float4(IN.UV.xy,1.0,1.0);

  return OUT;
}

PixelToFrame lightedPS(VertexToPixel IN) 
{
PixelToFrame OUT = (PixelToFrame)0;
float4 norm = normalize(IN.Normal);
float4 LightVector = normalize(xLightPosition-IN.Pos3D);
float LN = dot(LightVector, norm);
float4 R = normalize((2*LN)*norm-LightVector);
//float4 R = reflect(-LightVector, IN.Normal);
float4 V = normalize(eye-IN.Pos3D);
float RV = dot(R,V);
float RValpha = pow(RV, alpha);

float3 PixelColor = ambientK*ambientI;
//float3 PixelColor = (float3) 0.5;
if(LN > 0){
PixelColor += (diffuseK*diffuseI*LN);
//PixelColor = float3(1,1,1);
}
if(RV > 0){
PixelColor += (specularK*specularI*RValpha);
//PixelColor += (float3)RValpha;
}
OUT.Color = saturate(float4(PixelColor.xyz, 1));

return OUT;
}




// ------------ TECHNIQUES --------------

technique lighted{
	pass p0 {
		VertexShader = compile vs_3_0 lightedVS();
		PixelShader = compile ps_3_0 lightedPS();
	}
}


